package com.artqiyi.dollmachine.utils.manager;

import android.widget.Toast;

import com.artqiyi.dollmachine.common.Base.BaseApp;
import com.artqiyi.dollmachine.common.constant.ServerConstant;
import com.artqiyi.dollmachine.model.entity.CoinBuyBean;
import com.artqiyi.dollmachine.presenter.base.PresenterCallbackCollection;
import com.artqiyi.dollmachine.utils.Callback.MyStringCallback;
import com.artqiyi.dollmachine.utils.JSONUtils;
import com.artqiyi.dollmachine.utils.respone.PaymentRespone;
import com.artqiyi.dollmachine.utils.respone.WXPaymentRespone;
import com.tencent.mm.opensdk.modelpay.PayReq;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import timber.log.Timber;

/**
 * author: PengCheng
 * time: 2017/12/14 0014
 * desc:
 */

public class PayDialogManager {
    private static PayDialogManager instance;

    public static PayDialogManager getInstance() {
        if (instance == null) {
            synchronized (PayDialogManager.class) {
                if (instance == null) {
                    instance = new PayDialogManager();
                }
            }
        }
        return instance;
    }

    //生成支付交易单号
    public void getPay(CoinBuyBean bean) {
        Map<String, String> params = new HashMap<>();
        params.put("coin_id", String.valueOf(bean.getId()));
        HttpManager.postHttpRequest(ServerConstant.getPay(ConfigManager.getInstance().getConfigBean().getFake_pay()), params, new MyStringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Toast.makeText(BaseApp.getInstance(), e.getMessage(), Toast.LENGTH_SHORT).show();
                Timber.d("请求生成支付交易单号（模拟支付）失败");
            }

            @Override
            public void onResponse(String response, int id) {
                PaymentRespone paymentRespone = JSONUtils.jsonString2Bean(response, PaymentRespone.class);
                if (paymentRespone != null && paymentRespone.getData() != null) {
                    PaymentManager.getInstance().setPaymentId(paymentRespone.getData().getId());
                    getPayConfig(paymentRespone.getData().getId());
                }
            }
        });
    }

    //支付交易单号查询
    private void getPayConfig(int id) {
        HttpManager.getHttpRequest(ServerConstant.getPayConfig() + id + "/prepay-params", null, new MyStringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Timber.d("请求支付参数配置失败");
            }

            @Override
            public void onResponse(String response, int id) {
                WXPaymentRespone wXPaymentRespone = JSONUtils.jsonString2Bean(response, WXPaymentRespone.class);
                if (wXPaymentRespone != null && wXPaymentRespone.getData() != null) {
                    PayReq req = new PayReq();
                    req.appId = wXPaymentRespone.getData().getAppid();
                    req.partnerId = wXPaymentRespone.getData().getPartnerid();// 微信支付分配的商户号
                    req.prepayId = wXPaymentRespone.getData().getPrepayid();// 预支付订单号，app服务器调用“统一下单”接口获取
                    req.nonceStr = wXPaymentRespone.getData().getNoncestr();// 随机字符串，不长于32位，服务器小哥会给咱生成
                    req.timeStamp = wXPaymentRespone.getData().getTimestamp();// 时间戳，app服务器小哥给出
                    req.packageValue = "Sign=WXPay";// 固定值Sign=WXPay，可以直接写死，服务器返回的也是这个固定值
                    req.sign = wXPaymentRespone.getData().getSign();// 签名，服务器小哥给出，
                    BaseApp.mWxApi.sendReq(req);
                }
            }
        });
    }

}
